﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace BaseData
{
    /// <summary>
    /// 访问数据库的基类(扩展方法)
    /// </summary>
    public partial class DataConfig : Base
    {
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="businessType">业务类型</param>
        /// <param name="filePath">路径</param>
        /// <param name="dbName">数据库名称</param>
        /// <returns>是否成功</returns>
        public static bool Backup(BusinessType businessType, string filePath, string dbName)
        {
            bool isSuccess = true;
            DbParameter p = new System.Data.SqlClient.SqlParameter("@filePath", SqlDbType.VarChar, 500);
            p.Value = filePath;
            string sql = "BACKUP DATABASE " + dbName + " TO DISK =@filePath";
            BaseData.DataConfig.QueryNoRecords(businessType, sql, p);
            return isSuccess;
        }

        /// <summary>
        /// 恢复数据库
        /// </summary>
        /// <param name="businessType">业务类型</param>
        /// <param name="filePath">路径</param>
        /// <param name="dbName">数据库名称</param>
        /// <returns>是否成功</returns>
        public static bool Recovery(BusinessType businessType, string filePath, string dbName)
        {
            //先断开所有连接
            KillSpid(businessType, dbName);
            bool isSuccess = true;
            DbParameter p = new System.Data.SqlClient.SqlParameter("@filePath", SqlDbType.VarChar, 500);
            p.Value = filePath;
            string sql = "RESTORE  DATABASE " + dbName + " FROM DISK =@filePath WITH NOUNLOAD, REPLACE, STATS = 10 ";
            BaseData.DataConfig.QueryNoRecords(businessType, sql, p);
            return isSuccess;
        }

        /// <summary>
        /// 断开所有用户打开的连接
        /// </summary>
        /// <param name="businessType">业务类型</param>
        /// <param name="dbName">数据库名称</param>
        private static void KillSpid(BusinessType businessType, string dbName)
        {
            DbParameter p = new System.Data.SqlClient.SqlParameter("@dbname", SqlDbType.VarChar, 500);
            p.Value = dbName;
            #region 存储过程方式(备份)
            //BaseData.DataConfig.QueryNoRecordsByStoredProcedure(businessType, "Proc_KillConnection", p);
            #endregion
            string sql = "select * from master..sysprocesses  where dbid=db_id(@dbname) ";
            DataTable dtKillId = BaseData.DataConfig.QueryRecords(businessType, sql, p);
            if (dtKillId == null || dtKillId.Rows.Count == 0)
            {
                return;
            }
            string sqlKill = string.Empty;
            for (int i = 0; i < dtKillId.Rows.Count; i++)
            {
                sqlKill += "kill " + dtKillId.Rows[i]["spid"].ToString() + ";";
            }
            BaseData.DataConfig.QueryNoRecords(businessType, sqlKill);
        }
    }
}
